﻿using System;
using System.Collections.Generic;

namespace ProblemsSet
{
    public class Problem_32 : BaseProblem
    {
        public override object GetResult()
        {
            return MathLogic.GetSumOfInt(GetAllUnusual());
        }

        public static List<long> GetAllUnusual()
        {
            var res = new List<long>();
            //a*b=c
            for (var c = 1000; c < 10000; c++)
            {
                var hc = MathLogic.GetDigitSet(c);
                if (hc.Count < 4 || hc.Contains(0)) continue;
                for (var a = 1; a < 10000; a++)
                {
                    if (c % a != 0) continue;
                    var ha = MathLogic.GetDigitSet(a);
                    var ca = (int)(Math.Log10(a)) + 1;
                    if (ha.Count < ca) continue;
                    if (ha.Contains(0)) continue;
                    ha.UnionWith(hc);
                    if (ha.Count < 4 + ca) continue;
                    var b = c / a;
                    var cb = (int)(Math.Log10(b)) + 1;
                    if (cb != 5 - ca) continue;
                    var hb = MathLogic.GetDigitSet(b);
                    if (hb.Count < cb) continue;
                    if (hb.Contains(0)) continue;
                    ha.UnionWith(hb);
                    if (ha.Count < 9) continue;

                    res.Add(c);
                    break;
                }
            }
            return res;
        }

        public override string Problem
        {
            get
            {
                return @"We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once; for example, the 5-digit number, 15234, is 1 through 5 pandigital.

The product 7254 is unusual, as the identity, 39  186 = 7254, containing multiplicand, multiplier, and product is 1 through 9 pandigital.

Find the sum of all products whose multiplicand/multiplier/product identity can be written as a 1 through 9 pandigital.

HINT: Some products can be obtained in more than one way so be sure to only include it once in your sum.";
            }
        }

        public override bool IsSolved
        {
            get
            {
                return true;
            }
        }

        public override object Answer
        {
            get
            {
                return 45228;
            }
        }

    }
}
